將給予的數列 ls = [0, 1, 3, 6, 10]
分成不同部分的數列直到沒有任何 element,再相加
ls = [0, 1, 3, 6, 10]
ls = [1, 3, 6, 10]
ls = [3, 6, 10]
ls = [6, 10]
ls = [10]
ls = []
回傳每個部分數列的總和 (20, 20, 19, 16, 10, 0)
(ns sum-of-parts.core)
(defn parts-sums [ls]
(reductions - (reduce + ls) ls)
)
(reductions f coll)(reductions f init coll)
(reductions + [1 2 3]) ;; (1 3 6)
(reductions conj [] '(1 2 3)) ;; ([] [1] [1 2] [1 2 3])
(reduce f coll)
(reduce + [1 2 3]) ;; 6
(reduce + [1 2]) ;; 3